package demo1;

class Node<T>{
    T data;

    public void setData(T data){
        this.data = data;
    }
}

class StringNode extends Node<String>{
    @Override
    public void setData(String data) {
        super.setData(data);
    }

    // 在擦除机制中, 为了维护Java的多态性, 会自动生成桥接方法
    /*public void setData(Object data){
        setData((String) data);
    }*/

}

// 写一个泛型类, 有一个方法, 求一个T类型数组当中的最大值。 数组是没有指定的类型

class Alg<T extends Comparable<T>>{
    public T findMaxVal(T[] array){
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2{
    public static<T extends Comparable<T>> T findMaxVal(T[] array){
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}

class Alg3<T>{
    public <T extends Comparable<T>> T findMaxVal(T[] array){
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}

class Person{

}

class Student extends Person{

}

class MyArray1<E extends Person>{

}

class MyArrayNode<E extends Number>{

}

public class Test {

    public static void main(String[] args) {
        // demo1.Alg<Integer> alg = new demo1.Alg<>();
        Integer[] array = {1,12,13,15,61,81,9};
        //int ret = alg.findMaxVal(array);

        // int ret = demo1.Alg2.findMaxVal(array);

        Alg3<String> alg3 = new Alg3<>();
        int ret = alg3.<Integer>findMaxVal(array);
        System.out.println(ret);
    }

    public static void main4(String[] args) {
        MyArrayNode<Integer> myArrayNode1 = new MyArrayNode<>();
        MyArrayNode<Number> myArrayNode2 = new MyArrayNode<>();
        // demo1.MyArrayNode<String> stringMyArrayNode = new demo1.MyArrayNode<String>();
    }

    public static void main3(String[] args) {
        Node<String> node = new StringNode();
        node.setData("hello");

        MyArray1<Student> myArray1 = new MyArray1<>();
        MyArray1<Person> myArray11 = new MyArray1<>();
    }

    public static void main2(String[] args) {
        MyArray<Integer> myArray1 = new MyArray<Integer>();
        myArray1.set(0, 1);
        myArray1.set(1, 10);
        // myArray1.set(2, "hello"); // error

       int ret = myArray1.get(1);

    }

    public static void main1(String[] args) {
        MyArray myArray1 = new MyArray();
        myArray1.set(0, 1);
        myArray1.set(1, "hello");

        String ret = (String) myArray1.get(1);
    }
}
